<template>
    <div class="login">
      <el-form ref="loginForm" class="login-form" :model="loginForm" :rules="loginRules">
        <h3 class="title">超市货品信息管理系统</h3>
        <el-form-item prop="username">
          <el-input
            type="text"
            placeholder="账号"
            auto-complete="off"
            v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            auto-complete="off"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            type="text"
            placeholder="验证码"
            auto-complete="off"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.code"></el-input>
          <div class="login-code">
            <img :src="codeUrl"  class="login-code-img" @click="getCode"/>
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            @click.native.prevent="handleLogin"
            style="width:100%;"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <!--<el-upload style="text-align: center" :on-success="successHandle" :auto-upload="true"  :action="userKeyLogin" :show-file-list="false" :limit="1">
            <el-button type="text">数字签证登录</el-button>
          </el-upload>-->
        </el-form-item>
      </el-form>
      <div class="el-login-footer">
        <span>Copyright © 2025 project.vip All Rights Reserved.</span>
      </div>
    </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import { setToken } from '@/utils/auth'
export default {
  name: 'logIn',
  data () {
    return {
      // userKeyLogin: process.env.VUE_APP_BASE_URL + '/usbKeyLogin',
      loginForm: {
        username: '',
        password: '',
        code: '',
        uuid: '',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      codeUrl: '',
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      redirect: undefined
    }
  },
  created () {
    this.getCode()
  },
  watch:{
    $route:{
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate:true
    }
  },
  methods: {
    getCode () {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      })
    },
    successHandle(res) {
      if (res.code === 500) {
        this.getCode()
        this.$message.error(res.msg)
      } else {
        this.$message.success('登录成功')
        setTimeout(() => {
          setToken(res.token)
          this.$router.push({ path: this.redirect || '/index' })
        }, 2000)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.login{
  display: flex;
  background-image: url("../assets/images/login-background.gif");
  height: 100%;
  /*把背景图片放大到适合元素容器的尺寸，图片比例不变，但是要注意，超出容器的部分可能会裁掉。*/
  background-size: cover;
  /*在容器的中心对齐弹性项目*/
  justify-content: center;
  align-items: center;
  .login-form{
    width: 450px;
    border-radius: 6px;
    background: #fff;
    padding: 25px 25px 0px 25px;
  }
  .title{
    color: cornflowerblue;
    text-align: center;
    margin: 0 auto 30px;
  }
}
  .el-login-footer{
    /*元素的位置相对于浏览器窗口是固定位置。即使窗口是滚动的它也不会移动：*/
    position: fixed;
    bottom: 0px;
    color: white;
    font-size: 12px;
    letter-spacing: 1px;
    font-family: Arial;
  }
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
</style>
